<?php

namespace Apipad\Controller;
//商品
class GoodsController extends CommonController
{
    public function goods_lists(){
        $page=$detail['page']?$detail['page']:1;
        $limit=$detail['limit']?$detail['limit']:20;
        $model = D('UserGoods');
        $cate_id = I('cate_id');
        $map = [];
        if(I('keyword')){
            $map['goods_name|goods_sn'] = array('like','%'.I('keyword').'%');
        }
        if($cate_id){
            $cate_str = $this->get_child($cate_id,0);
            $map['_string'] = "FIND_IN_SET(cate_id,'".$cate_str."')";
        }
       
        $map['uid'] = UID;
        $map['isdel'] = 0;
        $count = $model->where($map)->count();
        $list = $model->where($map)->order('add_time desc')->page($page,$limit)->select();
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data']['count'] = $count;
        $data['data']['list'] = $list;
        exit(json_encode($data));
    }

    //仓库列表接口
    public function store_list(){
        $model = M('user_kuerp_warehouse');
        $loc_model = M('user_kuerp_warehouse_location');
        $list = $model->where(['uid'=>UID,'is_delete'=>0,'status'=>1])->select();
        foreach($list as $k=>$v){
            $loc_list = $loc_model->where(['uid'=>UID,'wh_id'=>$v['id'],'is_delete'=>0,'status'=>1])->select();
            $list[$k]['loc_list'] = $loc_list;
        }
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data']['list'] = $list;
        exit(json_encode($data));
    }

    //商品列表
    public function index(){
        $detail = I('');
        $cate_id = $detail['cate_id'];
        if(empty($cate_id)){
            $data['code'] = 0;
            $data['msg'] = '分类不能为空';
            exit(json_encode($data));
        }
        //类型判断是否为当前客户
        $cat_model = D('UserMysqlCate');
        $cat_info = $cat_model->where(['id'=>$cate_id,'stype'=>0,'uid'=>UID,'isdel'=>0])->find();
        if(empty($cat_info)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '当前类型不属于当前客户';
            exit(json_encode($data_cc));
        }
        $page=$detail['page']?$detail['page']:1;
        $limit=$detail['limit']?$detail['limit']:20;
        $model = D('UserGoods');
        $cate_model = D('UserMysqlCate');
        $cate_lists = $cate_model->getChildIds($cate_id);
        $cate_lists[]=trim($cate_id);
        $cate_str = implode(',',$cate_lists);
        $map = [];
        if(I('keyword')){
            $map['goods_name|goods_sn|goods_attr'] = array('like','%'.I('keyword').'%');
        }
        $map['_string'] = "FIND_IN_SET(cate_id,'".$cate_str."')";
        $map['uid'] = UID;
        $map['isdel'] = 0;
        $count = $model->where($map)->count();
        $list = $model->where($map)->order('add_time desc')->page($page,$limit)->select();
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data']['count'] = $count;
        $data['data']['list'] = $list;
        exit(json_encode($data));
    }
      //查询大类和子分类
    function get_child($cate_id,$type){
        $user_mysql_cate=M("user_mysql_cate"); 
        $uid=UID;
        $list=$user_mysql_cate->where(['pid'=>$cate_id,'uid'=>$uid,'stype'=>$type])->select();
        $mcate=$cate_id.",";
        if($list){
             foreach($list as $k=>$val){
                $mcate.=$val['id'].",";
             }
        }
        $mcate=rtrim($mcate,',');
        return $mcate;
    }
    //添加
    public function add(){
        $uid=UID;
        $user_goods=D("UserGoods"); 
        $user_goods_attr=M("user_goods_attr");
        $s_model = D('UserKuerpWarehouse');
        $detail = I('post.');
        $goods_info['uid']=$uid;
        if(empty($detail['goods_name'])){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写商品名称';
            exit(json_encode($data_cc));
        }
        if(empty($detail['cate_id'])){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请选择分类';
            exit(json_encode($data_cc));
        }
        if(empty($detail['goods_sn'])){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写货号';
            exit(json_encode($data_cc));
        }
        if(empty($detail['chenfen'])){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写成分';
            exit(json_encode($data_cc));
        }
        if(empty($detail['goods_attr'])){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写版号';
            exit(json_encode($data_cc));
        }
        if(empty($detail['unit'])){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写单位';
            exit(json_encode($data_cc));
        }
        if(empty($detail['goods_color'])){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写颜色';
            exit(json_encode($data_cc));
        }
        if(empty($detail['guige_mame'])){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写规格';
            exit(json_encode($data_cc));
        }
        if(empty($detail['goods_price'])){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写销售单价';
            exit(json_encode($data_cc));
        }
        //类型判断是否为当前客户
        $cat_model = D('UserMysqlCate');
        $cat_info = $cat_model->where(['id'=>$detail['cate_id'],'stype'=>0,'uid'=>UID,'isdel'=>0])->find();
        if(empty($cat_info)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '当前类型不属于当前客户';
            exit(json_encode($data_cc));
        }
        $color=$detail['color'];
        $guige=$detail['guige'];
        $price=$detail['price'];
        $dis_price=$detail['dis_price'];
        $goods_num=$detail['goods_num'];
        $store_id=$detail['store_id'];
        $store_kuwei_id=$detail['store_kuwei_id'];
        $user_goods->startTrans();
        $goods_info['cate_id']=$detail['cate_id']?:0;
        $goods_info['goods_name']=$detail['goods_name']?:"";
        $goods_info['goods_sn']=$detail['goods_sn'];
        $goods_info['goods_attr']=$detail['goods_attr'];
        $goods_info['unit']=$detail['unit'];
        $goods_info['goods_color']=$detail['goods_color'];
        $goods_info['guige_mame']=$detail['guige_mame'];
        $goods_info['goods_price']=$detail['goods_price'];
        $goods_info['suppler_name']=$detail['suppler_name'];
        $goods_info['suppler_price']=$detail['suppler_price'];
        // $goods_info['store_name']=$detail['store_name'];
        // $goods_info['store_kuwei']=$detail['store_kuwei'];
        $goods_info['chenfen']=$detail['chenfen'];
        $goods_info['warning_number']=$detail['warning_number'];
        $goods_info['goods_img']=$detail['goods_img'];
        $goods_info['goods_video']=$detail['goods_video'];

        $goods_info['add_time']=time();
        $goods_id=$user_goods->add($goods_info);
       
        $order_nums = 0;
        foreach($goods_num as $gk=>$gv){
            $order_nums = bcadd($order_nums,$gv,0);
        }

        $o_model = M('UserKuerpWarehouseOrders');
        $o_data['uid'] = UID;
        $o_data['type'] = 1;
        $o_data['product_id'] = $goods_id;
        $o_data['goods_name'] = $detail['goods_name'];
        $o_data['goods_sn'] = $detail['goods_sn'];
        $o_data['goods_attr'] = $detail['goods_attr'];
        $o_data['chenfen'] = $detail['chenfen'];
        $o_data['unit'] = $detail['unit'];
        $o_data['nums'] = $order_nums;
        $o_data['add_time'] = time();
        $order_id = $o_model->add($o_data);
        if(!$order_id){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '添加订单失败';
            exit(json_encode($data_cc));
        }
        if($goods_id){
            foreach($color as $k=>$val){
                if(empty($val)){
                    $data_cc['code'] = 0;
                    $data_cc['msg'] = "颜色不能为空";
                    exit(json_encode($data_cc));
                }
                if(empty($guige[$k])){
                    $data_cc['code'] = 0;
                    $data_cc['msg'] = "规格不能为空";
                    exit(json_encode($data_cc));
                }
                if($goods_num[$k]>0){
                    if(empty($store_id[$k])){
                        $data_cc['code'] = 0;
                        $data_cc['msg'] = "请选择仓库";
                        exit(json_encode($data_cc));
                    }
                    if(empty($store_kuwei_id[$k])){
                        $data_cc['code'] = 0;
                        $data_cc['msg'] = "请选择库位";
                        exit(json_encode($data_cc));
                    }
                }
                $dva['goods_id']=$goods_id;
                $dva['goods_sn']=$detail['goods_sn'];
                $dva['goods_attr']=$detail['goods_attr'];
                $dva['chenfen']=$detail['chenfen'];
                $dva['unit']=$detail['unit'];
                $dva['goods_name']=$detail['goods_name'];
                $dva['color']=$val;
                $dva['uid'] = UID;
                $dva['guige_name']=$guige[$k];
                $dva['price']=$price[$k];
                $dva['dis_price']=$dis_price[$k];
                // $dva['goods_num']=$goods_num[$k];
                // $dva['store_id']=$store_id[$k];
                // $dva['store_kuwei_id']=$store_kuwei_id[$k];
                $dva['add_time']=time();
                $goods_attr_id=$user_goods_attr->add($dva);
                if($goods_attr_id){
                    if($goods_num[$k]>0){
                        $ru_data = [];
                        $ru_data['uid'] = UID;
                        $ru_data['order_id'] = $order_id;
                        $ru_data['product_id'] = $goods_id;
                        $ru_data['item_id'] = $goods_attr_id;
                        $ru_data['wh_id'] = $store_id[$k];
                        $ru_data['loc_id'] = $store_kuwei_id[$k];
                        $ru_data['batch_no'] = 1;
                        $ru_data['nums'] =$goods_num[$k];
                        $ru_data['remark'] = '添加商品时自动入库';
                        $s_result =  $s_model->into($ru_data);
                        if(!$s_result['status']){
                            $data_cc['code'] = 0;
                            $data_cc['msg'] = $s_result['msg'];
                            exit(json_encode($data_cc));
                        }
                    }
                }else{
                    $data_cc['code'] = 0;
                    $data_cc['msg'] = "添加失败";
                    exit(json_encode($data_cc));
                }
            }
            $user_goods->commit();
            $data_cc['code'] = 200;
            $data_cc['msg'] = "添加成功";
            $data_cc['data'] = $goods_id;
            exit(json_encode($data_cc));
        }else{
            $data_cc['code'] = 0;
            $data_cc['msg'] = "添加失败";
            exit(json_encode($data_cc));
        }
    }
    //详情
    public function detail(){
        $detail = I('');
        $goods_id = $detail['goods_id'];
        if(empty($goods_id)){
            $data['code'] = 0;
            $data['msg'] = '商品ID不能为空';
            exit(json_encode($data));
        }
        $model = D('UserGoods');
        $map['id'] = $goods_id;
        $map['uid'] = UID;
        $map['isdel'] = 0;
        $info = $model->where($map)->find();
        if($info){
            $data['code'] = 200;
            $data['msg'] = '查询成功';
            $data['data'] = $info;
            exit(json_encode($data));
        }else{
            $data['code'] = 0;
            $data['msg'] = '数据不存在';
            exit(json_encode($data));
        }
    }
    //编辑 --编辑修改详情的数据
    public function edit(){
        $user_goods=M("user_goods"); 
        $user_goods_attr=M("user_goods_attr"); 
        $detail = I('post.');
        $goods_id = $detail['goods_id'];
        if(empty($goods_id)){
            $data['code'] = 0;
            $data['msg'] = '商品ID不能为空';
            exit(json_encode($data));
        }
        if(empty($detail['goods_name'])){
            $data['code'] = 0;
            $data['msg'] = '请填写商品名称';
            exit(json_encode($data));
        }
        if(empty($detail['cate_id'])){
            $data['code'] = 0;
            $data['msg'] = '请选择分类';
            exit(json_encode($data));
        }
        if(empty($detail['goods_sn'])){
            $data['code'] = 0;
            $data['msg'] = '请填写货号';
            exit(json_encode($data));
        }
        if(empty($detail['chenfen'])){
            $data['code'] = 0;
            $data['msg'] = '请填写成分';
            exit(json_encode($data));
        }
        if(empty($detail['goods_attr'])){
            $data['code'] = 0;
            $data['msg'] = '请填写版号';
            exit(json_encode($data));
        }
        if(empty($detail['unit'])){
            $data['code'] = 0;
            $data['msg'] = '请填写单位';
            exit(json_encode($data));
        }
        if(empty($detail['goods_price'])){
            $data['code'] = 0;
            $data['msg'] = '请填写销售单价';
            exit(json_encode($data));
        }
        $user_goods->startTrans();
        $goods_info['cate_id']=$detail['cate_id']?:0;
        $goods_info['goods_name']=$detail['goods_name']?:"";
        $goods_info['goods_sn']=$detail['goods_sn'];
        $goods_info['goods_attr']=$detail['goods_attr'];
        $goods_info['unit']=$detail['unit'];
        $goods_info['goods_price']=$detail['goods_price'];
        $goods_info['suppler_name']=$detail['suppler_name'];
        $goods_info['suppler_price']=$detail['suppler_price'];
        $goods_info['chenfen']=$detail['chenfen'];
        $goods_info['warning_number']=$detail['warning_number'];
        $goods_info['goods_img']=$detail['goods_img'];
        $goods_info['goods_video']=$detail['goods_video'];
        $goods_info['add_time']=time();
        $result =$user_goods->where(['id'=>$goods_id,'uid'=>UID])->save($goods_info);
        if($result===false){
            $this->ajaxErrReturn('编辑失败');
        }
        //规格信息修改
        $ga_wdata['goods_id'] = $goods_id;
        $ga_wdata['uid'] = UID;
        $ga_sdata['goods_sn']= $detail['goods_sn'];
        $ga_sdata['goods_attr']= $detail['goods_attr'];
        $ga_sdata['unit']= $detail['unit'];
        $ga_sdata['chenfen']= $detail['chenfen'];
        $ga_sdata['goods_name']= $detail['goods_name'] ? : "";
        $result = $user_goods_attr->where($ga_wdata)->save($ga_sdata);
        if($result===false){
            $this->ajaxErrReturn('编辑失败2');
        }
        //规格修改
        if($detail['skuid']){
            foreach($detail['skuid'] as $key=>$skuid){
                $ga_wdata2['id'] = $skuid;
                $ga_wdata2['uid'] = UID;
                $ga_sdata2['price'] = $detail['price'][$key];
                $ga_sdata2['dis_price'] = $detail['dis_price'][$key];
                $result = $user_goods_attr->where($ga_wdata2)->save($ga_sdata2);
                if($result===false){
                    $this->ajaxErrReturn('编辑失败3');
                }
            }
        }
        //规格新增
        if($detail['add_color'] && $detail['add_guige'] && $detail['add_price'] && $detail['add_dis_price']){
            $color = $detail['add_color'];
            $guige = $detail['add_guige'];
            $price = $detail['add_price'];
            $dis_price = $detail['add_dis_price'];
            foreach($color as $k=>$val){
                $dva = [];
                $dva['uid'] = UID;
                $dva['goods_id'] = $goods_id;
                $dva['color'] = $val;
                $dva['guige_name'] = $guige[$k];
                $wdva = $dva;
                $wdva['isdel'] = 0;
                $count = $user_goods_attr->where($wdva)->count();
                if($count>0){
                    $this->ajaxErrReturn('规格`'.$guige[$k].'/'.$val.'`已存在');
                }
                $dva['goods_sn'] = $detail['goods_sn'];
                $dva['goods_attr'] = $detail['goods_attr'];
                $dva['chenfen']=$detail['chenfen'];
                $dva['unit'] = $detail['unit'];
                $dva['goods_name'] = $detail['goods_name'];
                $dva['price'] = $price[$k];
                $dva['dis_price'] = $dis_price[$k];
                $dva['add_time'] = time();
                $goods_attr_id = $user_goods_attr->add($dva);
                if(!$goods_attr_id){
                    $this->ajaxErrReturn('规格添加失败');
                }
            }
        }
        if($result!==false) {
            $user_goods->commit();
            $data_cc['code'] = 200;
            $data_cc['msg'] = "编辑成功";
            $data_cc['data'] = $goods_id;
            exit(json_encode($data_cc));
        } else {
            $data_cc['code'] = 0;
            $data_cc['msg'] = "编辑失败";
            exit(json_encode($data_cc));
        }
    }
    //删除
    public function del(){
        $detail = I('');
        $goods_id = $detail['goods_id'];
        if(empty($goods_id)){
            $data['code'] = 0;
            $data['msg'] = '商品ID不能为空';
            exit(json_encode($data));
        }
        $model = D('UserGoods');
        $d_model = M('user_goods_attr');
        //存在其他的进行规避
        $map['id'] = $goods_id;
        $map['uid'] = UID;
        $map['isdel'] = 0;
        $info = $model->where($map)->find();
        if(empty($info)){
            $data['code'] = 0;
            $data['msg'] = '信息不存在';
            exit(json_encode($data));
        }
        $s_model = D('UserKuerpWarehouseLocProduct');
        $s_where['uid']=UID;
        $s_where['product_id'] = $goods_id;
        $s_info = $s_model->where($s_where)->select();
        if($s_info){
            $data['code'] = 0;
            $data['msg'] = '存在仓库数据,不进行删除';
            exit(json_encode($data));
        }
        $s_data['isdel'] = 1;
        $s_data['del_time'] = time();
        $model->startTrans();
        $result = $model->where(['id'=>$goods_id,'uid'=>UID])->save($s_data);
        if($result){
            $res = $d_model->where(['goods_id'=>$goods_id,'uid'=>UID])->save($s_data);
            if($res){
                $model->commit();
                $data['code'] = 200;
                $data['msg'] = '删除成功';
                exit(json_encode($data));
            }else{
                $data['code'] = 0;
                $data['msg'] = '删除失败';
                exit(json_encode($data));
            }
        }else{
            $data['code'] = 0;
            $data['msg'] = '删除失败';
            exit(json_encode($data));
        }
    }
    
}
